package com.vogue.supplymall.match.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.vogue.supplymall.account.domain.service.BaseCodeService;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.common.domain.validate.Regex;
import com.vogue.supplymall.match.domain.dto.BaseDTO;
import com.vogue.supplymall.match.domain.dto.PcrapDTO;
import com.vogue.supplymall.match.domain.dto.PfcrapDTO;
import com.vogue.supplymall.match.domain.dto.PullShopOfCustomerDTO;
import com.vogue.supplymall.match.domain.service.ColValidateService;
import com.vogue.supplymall.match.domain.service.MatchOrganService;
import com.vogue.supplymall.match.domain.service.PrepayService;
import com.vogue.supplymall.order.domain.service.SupplierConsumerService;

@RestController
public class PrepayController extends BaseController{
	@Resource
	private MatchOrganService matchOrganService;
	@Autowired
	private ColValidateService colValidateService;
	@Autowired
	private PrepayService prepayService;
	@Autowired
    private MessageSourceUtils msu;
	@Autowired
	private SupplierConsumerService supplierConsumerService;
	@Autowired
	private BaseCodeService baseCodeService;
	
	
	/**
	 * 根据注册号得到买家门店信息
	 */
	@RequestMapping(value = "/ns/{appver}/match/pullShopOfCustomer", method = RequestMethod.POST)
	public String pullShopOfCustomer(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("appver") @Regex(name = "appver", regexcontent = "vernum") String appver,
			@RequestBody(required = true)  BaseDTO<PullShopOfCustomerDTO> baseDTO) {
		
		boolean resultFlag=true;
		String message=msu.getMessage("global.system.success", null);
		Map<String, Object> resultMap=new HashMap<>();
		List<Map<String, Object>> errList=new ArrayList<>();
		List<Map<String, Object>> resultList=new ArrayList<>();
		
		//首先对数据进行校验
		try {
 			errList = colValidateService.pullShopOfCustomerValidate(baseDTO);
		} catch (Exception e2) {
			resultFlag=false;
			message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
		}
		
		if(errList.size()>0) {
			resultFlag=false;
			message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
		}else {
			try {
				//得到客户下的门店信息
				String syscodes=baseDTO.getData().getSyscodes();
				if(StringUtils.isNoneBlank(syscodes)) {
					resultList=prepayService.getShopOfCustomer(syscodes);					
				}else {
					message="注册号不能为空";
				}
			} catch (Exception e1) {
				resultFlag=false;
				message=msu.getMessage("match.validate.pullShopFail", new String[]{});
			}
			
		}
		
		//返回数据
		resultMap.put("result", resultFlag);
		resultMap.put("message", message);
		resultMap.put("details", errList);
		resultMap.put("data", resultList);
		String json=new Gson().toJson(resultMap);
		
		return json;
	}
	
	/**
	 * 推送买家支付信息
	 */
	@RequestMapping(value = "/ns/{appver}/match/pushCustomerRelationAndPayMode", method = RequestMethod.POST)
	public String pushCustomerRelationAndPayMode(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("appver") @Regex(name = "appver", regexcontent = "vernum") String appver,
			@RequestBody(required = true)  BaseDTO<PfcrapDTO> baseDTO) {
		
		boolean resultFlag=true;
		String message=msu.getMessage("global.system.success", null);
		Map<String, Object> resultMap=new HashMap<>();
		List<Map<String, Object>> errList=new ArrayList<>();
		List<Map<String, Object>> resultList=new ArrayList<>();
		
		//首先对数据进行校验
		try {
 			errList = colValidateService.pushCustomerRelationAndPayModeValidate(baseDTO);
		} catch (Exception e2) {
			resultFlag=false;
			message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
		}
		
		if(errList.size()>0) {
			resultFlag=false;
			message=msu.getMessage("match.validate.uperr.datastructure", new String[]{});
		}else {
			
			try {
				if(resultFlag){
					//保存支付方式
					Map<String, Object> map=supplierConsumerService.saveCustomerRelationAndPayMode(baseDTO);
					resultFlag=(boolean) map.get("resultFlag");
					message=(String) map.get("message");
					
				}
			} catch (Exception e1) {
				resultFlag=false;
				message=msu.getMessage("match.validate.pushPayModeFail", new String[]{});
			}
			
		}
		
		//返回数据
		resultMap.put("result", resultFlag);
		resultMap.put("message", message);
		resultMap.put("details", errList);
		resultMap.put("data", resultList);
		String json=new Gson().toJson(resultMap);
		
		return json;
	}
	
}
